<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form label-width="220px">
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="工号">
            <el-input v-model="userInfo.workNumber" class="inputW" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="入职时间">
            <!-- 数据"2021-01-01" =>影响视图显示
                   视图选择 -> 默认绑定日期对象 -> v-model变量

                   type="date" (选择年-月-日)控制选择日期格式(组件渲染内容)
                   value-format 选择的值绑定格式(默认不写,v-model绑定的是日期对象)
               -->
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              style="width: 300px"
              type="date"
              class="inputW"
            />
            <!-- value-format="YYYY-MM-DD" -->
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 姓名 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="姓名">
            <el-input v-model="userInfo.username" class="inputW" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门">
            <el-input
              readonly
              v-model="userInfo.departmentName"
              class="inputW"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!--手机 聘用形式  -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" style="width: 300px" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="聘用形式">
            <el-select v-model="userInfo.formOfEmployment" class="inputW">
              <el-option
                v-for="item in EmployeeEnum.hireType"
                :key="item.id"
                :label="item.value"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <upload-img ref="imgUrl"></upload-img>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="saveUser">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { sysUser, updateEmployeesAPI } from '@/api'
import EmployeeEnum from '@/api/constant'

export default {
  data () {
    return {
      userId: this.$route.query.id,
      EmployeeEnum, // 员工枚举数据
      userInfo: {}
      //   知识点:v-model="userInfo.workNumber"
      // 当输入框有值的时候
      // 如果对象里有这个属性,则赋值
      // 如果对象里无这个属性,则会添加属性并赋值
    }
  },
  created () {
    // 调用获取用户信息的 API
    this.loadUserInfoById()
  },
  methods: {
    // 数据回显
    async loadUserInfoById () {
      const res = await sysUser(this.$route.query.id)
      this.userInfo = res.data
      //   给"对象后续添加一个属性"的时候,"还想双向绑定好用"不会应该对象本身的响应式触发
      //   this.userInfo.formOfEmployment = parseInt(this.$route.query.form)
      // 解决:如果你要后续给对象添加属性
      // $set() Vue内部提供的一个专门更新数组/对象某个值的,(并额外添加数据劫持)
      // 参数一:数组/对象 目标
      // 参数2:下标/属性名
      // 参数三:值
      this.$set(
        this.userInfo,
        'formOfEmployment',
        parseInt(this.$route.query.form)
      )
      this.$refs.imgUrl.imageUrl = res.data.staffPhoto
    },
    //保存更新按钮 -> 确认修改把头像地址保存到userInfo里一起带给后台
    async saveUser () {
      this.userInfo.staffPhoto = this.$refs.imgUrl.imageUrl
      const res = await updateEmployeesAPI(this.userInfo)
      this.$message.success(res.message)
    }
  }
}
</script>

<style scoped lang="scss"></style>
